/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  Format.java                                      
 *  File Contents Outline :  格式统一                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */

package cn.edu.thu.log.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

public class Format {

    /**
     * setup timestamp in Date format
     *
     * @param timeString String of timeStamp
     * @return Date format as timeStamp
     */
    public static Date StringToTimeStamp(String timeString) {
        Date timeStamp = new Date();
        if (timeString != null) {
            try {
                timeString = timeString.trim();
                timeStamp.setYear(Integer.parseInt(timeString.substring(0, 4)) - 1900);
                timeStamp.setMonth(Integer.parseInt(timeString.substring(4, 6)) - 1);
                timeStamp.setDate(Integer.parseInt(timeString.substring(6, 8)));
                timeStamp.setHours(Integer.parseInt(timeString.substring(8, 10)));
                timeStamp.setMinutes(Integer.parseInt(timeString.substring(10,
                        12)));
                timeStamp.setSeconds(Integer.parseInt(timeString.substring(12,
                        14)));
            } catch (Exception e) {
      //          System.out.print("\ntime format wrong as:" + timeString);

            }
        }
        return timeStamp;
    }

    public static Date DateStringToDate(String timeString) {
        //  System.out.println("before:" + timeString);
//        "2012-03-01T09:04:31.282+08:00";
        Date date = new Date();

        if (timeString != null) {
            try {
                String[] timeStrings = timeString.split("T");
                String datePart = timeStrings[0];
                String[] dateStrings = datePart.split("-");
                String yearString = dateStrings[0];
                String monthString = dateStrings[1];
                String dayString = dateStrings[2];

                String timePart = timeStrings[1];
                int index = timePart.indexOf("+");
                // System.out.println("timePart:"+timePart);
                //  System.out.println("index:"+index);

                String time1 = timePart.substring(0, index);
                index = time1.indexOf(".");
                // System.out.println("timePart:"+timePart);
                // System.out.println("index:" + index);
                //  String[] timeMinis = time1.split(".");
                String time;
                if (index != -1) {
                    time = time1.substring(0, index);
                } else {
                      time = time1;
                  
                }
                String miniString = time1.substring(index + 1);
                String[] time2 = time.split(":");
                String hourString = time2[0];
                String minuteString = time2[1];
                String secondString = time2[2];

                // date = new Date();
//            System.out.println("yearString:" + yearString);
//            System.out.println("monthString:" + monthString);
//            System.out.println("dayString:" + dayString);
//            System.out.println("hourString:" + hourString);
//            System.out.println("minuteString:" + minuteString);
//            System.out.println("secondString:" + secondString);
//            System.out.println("miniString:" + miniString);
                date = new Date();
                date.setYear(Integer.parseInt(yearString) - 1900);
                date.setMonth(Integer.parseInt(monthString) - 1);
                date.setDate(Integer.parseInt(dayString));
                date.setHours(Integer.parseInt(hourString));
                date.setMinutes(Integer.parseInt(minuteString));
                date.setSeconds(Integer.parseInt(secondString));

            } catch (Exception e) {
                System.out.print("\ntime format wrong as:" + timeString);

            }

        }
        //  System.out.println("after:"+date);
        return date;
    }

    /**
     * split content by Tokenizer to List
     *
     * @param logHeadTokenizer Tokenizer
     * @param logHeadContent content
     * @return a list of content fields
     */
    public static ArrayList<String> splitContentToList(String tokenizer,
            String content) {

        // Split logHeadContent by logHeadTokenizer
        int len = content.length();
        int pos = 0;
        ArrayList<String> alParams = new ArrayList<String>();
        int nl = tokenizer.length();
        int oldPos = 0;
        while (pos < len) {
            int idx = content.indexOf(tokenizer, pos);
            if (idx != -1) {
                pos = idx + nl;

                if (idx == 0) {
                    alParams.add("");
                    oldPos = pos;
                } else if (content.charAt(idx - 1) != '\\') {
                    alParams.add(content.substring(oldPos, idx));
                    oldPos = pos;
                }

                if (pos == len) {
                    alParams.add("");
                }
            } else {
                alParams.add(content.substring(pos + nl));
                pos = len;
            }
        }

        return alParams;
    }

    /**
     * split content by Tokenizer to List
     *
     * @param logHeadTokenizer Tokenizer
     * @param logHeadContent content
     * @return a list of content fields
     */
    public static Hashtable<String, String> splitContentToMap(String tokenizer,
            String content) {
        //   System.out.println("content:"+content);
        // Split logHeadContent by logHeadTokenizer
        int len = content.length();
        int pos = 0;
        Hashtable<String, String> alParamMap = new Hashtable();
        //ArrayList<String> alParams = new ArrayList<String>();
        int nl = tokenizer.length();
        int oldPos = 0;
        while (pos < len) {
            int idx = content.indexOf(tokenizer, pos);
            if (idx != -1) {
                pos = idx + nl;

                if (idx == 0) {
                    alParamMap.put("", "");
                    //alParams.add("");
                    oldPos = pos;
                } else if (content.charAt(idx - 1) != '\\') {
                    String tempField = content.substring(oldPos, idx);
                    String[] tempFields;
                    String abbre = "";
                    String value = "";
                    if (tempField.contains(":")) {

                        tempFields = splitLogBodyField(tempField);
                        abbre = tempFields[0];
                        if (tempFields.length > 1) {
                            value = tempFields[1];
                        }
                    }
                    alParamMap.put(abbre, value);
                    // alParams.add(content.substring(oldPos, idx));
                    oldPos = pos;
                }

                if (pos == len) {
                    String tempField = content.substring(oldPos - 1, pos);
                    String[] tempFields;
                    String abbre = "";
                    String value = "";
                    if (tempField.contains(":")) {
                        tempFields = splitLogBodyField(tempField);
                        abbre = tempFields[0];
                        if (tempFields.length > 1) {
                            value = tempFields[1];
                        }
                    }
                    alParamMap.put(abbre, value);
                    //   alParamMap.put("", "");

                }
            } else {
                int lastIndex = content.lastIndexOf(tokenizer);
                String tempField = content.substring(lastIndex + 1);
                String[] tempFields;
                String abbre = "";
                String value = "";
                if (tempField.contains(":")) {

                    tempFields = splitLogBodyField(tempField);
                    if (tempFields.length != 0) {
                        abbre = tempFields[0];
                    }
                    if (tempFields.length > 1) {
                        value = tempFields[1];
                    }
                }
                alParamMap.put(abbre, value);
                //alParams.add(content.substring(pos + nl));
                pos = len;
            }
        }
//       Enumeration enu =alParamMap.keys();
//       while(enu.hasMoreElements()){
//      String key= (String) enu.nextElement();
//      String value=alParamMap.get(key);

//       }

        return alParamMap;
    }

    static private String[] splitLogBodyField(String logBodyField) {
        String[] logBodyDetail = new String[2];
        int tempIndex = logBodyField.indexOf(":");
      //  System.out.println("tempIndex:" + tempIndex);
        logBodyDetail[0] = logBodyField.substring(0, tempIndex);
        //  System.out.println("logBodyDetail[0]:"+logBodyDetail[0]);
        logBodyDetail[1] = logBodyField.substring(tempIndex + 1);
        //    System.out.println("logBodyDetail[1]:"+logBodyDetail[1]);
        return logBodyDetail;
    }
}
